﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.ProductSKU
{
    public partial class ModifyProductCategory : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected ShopProductInfo product;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);
            try
            {
                string pno = Request["pno"];
                string cid = Request["cid"];
                //批量删除
                if (string.IsNullOrEmpty(pno) || string.IsNullOrEmpty(cid))
                {
                    throw new Exception("参数错误");
                }
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    product = session.CreateQuery("from ShopProductInfo where ProductNo=:pno ").SetParameter("pno", pno).UniqueResult<ShopProductInfo>();

                    if (product == null)
                    {
                        throw new Exception("修改失败!");
                    }

                    var tree = session.Get<ShopCategory>(int.Parse(cid));

                    if (tree == null)
                    {
                        throw new Exception("修改失败!");
                    }

                    product.TypeId = tree.Id;
                    product.TypePrefix = tree.Prefix;

                    session.Update(product);

                    Session[GlobalSessionString.CategoryIdBeforeModify] = cid;
             
                    session.Flush();
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(
                    new ReturnCode()
                    {
                        statusCode = 300,
                        message = "操作失败-" + ex.Message
                    }
                    );
                return;
            }

            _framePage.Alert(
                new ReturnCode()
                {
                    statusCode = 200,
                    message = "修改成功!",
                    navTabId = _framePage.NavTabId,
                    callbackType = "forward",
                    forwardUrl = "/systemmanage/Basics/ProductSKU/productEdit.aspx?pid=" + product.Id
                }
                );
        }
    }
}